#include"../head/page0.h"
#include"../head/find_way.h"
lv_obj_t *mybox = NULL;
int num = 0;
char old_str[64];
lv_font_t *chinese_font = NULL;
lv_obj_t *page1;
lv_obj_t *page_admin2=NULL;
lv_obj_t *add_picture_page=NULL;
lv_obj_t *add_picture_file_path_use=NULL;

static lv_obj_t *ta2;
static bool pwd_hidden = true;  // 初始状态：隐藏

int usersave; //保存当前登录的用户------这里usersave每次登录会被覆盖！
int count=0;
user users[MAX_USER_NUM]; 
int save=1;
lv_obj_t *msgbox=NULL;
lv_obj_t *orig_scr=NULL;  // 获取当前正在显示的屏幕
lv_obj_t *kb=NULL;
lv_obj_t *kb3=NULL;
lv_obj_t *kb3_1=NULL;

//--------------------------------为每个用户增加flag 是否买过票 
void get_user_file(void){
  FILE *fp;
  if((fp=fopen("/picture/user.txt","r+"))){
    while(fscanf(fp,"%s %s",users[count].name,users[count].password)==2){
      count++;
      users[count].flag=0;
    }
  }else{
    perror("open user.txt error");
    fflush(stdout);
    return ;
  }
  fclose(fp);
  printf("读到%d个用户\n",count);
  for(int i=0;i<count;i++){
    printf("%s %s\n",users[i].name,users[i].password);
  }
  fflush(stdout);
  return ;
}
void push_user_file(user u){
  FILE *fp=fopen("/picture/user.txt","a+");
  if(!fp){
    perror("open user.txt error");
    fflush(stdout);
    return ;
  }
  fprintf(fp,"%s %s\n",u.name,u.password);
  fclose(fp);
  return ;
}


void create_page1(void){
  page1=lv_obj_create(NULL);
  lv_obj_set_size(page1,LV_HOR_RES,LV_VER_RES);
  //lv_obj_t *label = lv_label_create(page1);
  lv_scr_load(page1);  
  show_image_auto("/picture/p1.bmp");
  create_page1_ui();


  lv_obj_t *btn2=lv_btn_create(lv_scr_act());
  lv_obj_set_size(btn2,100,50);
  lv_obj_t *label2=lv_label_create(btn2);
  lv_label_set_text(label2,"返回");
  lv_obj_set_style_text_font(label2,chinese_font,0);
  lv_obj_align(btn2,LV_ALIGN_TOP_RIGHT,0,0);
  lv_obj_add_event_cb(btn2, return_main_page, LV_EVENT_CLICKED, NULL);
}

void return_main_page(lv_event_t *e){

  lv_scr_load(orig_scr);
}


//登录按钮
void btn_event_cb(lv_event_t *e) {
    login_input_t *inputs = lv_event_get_user_data(e);
    const char *username = lv_textarea_get_text(inputs->ta_username);
    const char *password = lv_textarea_get_text(inputs->ta_password);
    
    bool flag = false;
    for(int i=0;i<10;i++){
        if(strcmp(username, users[i].name) == 0) {
            if(strcmp(password, users[i].password) == 0){
              if(strcmp(username,"admin")==0){
                //create_page_admin();
                create_page_admin_2();
                printf("admin login success\n");
                fflush(stdout);
              }else{//登录成功
                create_page1();
                printf("login success\n");
                flag = true;
                usersave=i;
              }
                

                              

                return;
            } else {
                static const char *btns[] = {"ok", ""};
                lv_obj_t *mbox=lv_msgbox_create(lv_scr_act(),"login","密码错误",btns,true);
                lv_obj_align(mbox,LV_ALIGN_TOP_MID,0,100);
                lv_obj_set_style_text_font(mbox, chinese_font, 0);
                lv_obj_center(mbox);
                lv_obj_add_event_cb(mbox,mbox_ok_event_cb,LV_EVENT_VALUE_CHANGED,NULL);
                printf("password error\n");

                return;
            }
        }
    }
    if(!flag) {
        lv_obj_t *mbox=lv_msgbox_create(lv_scr_act(),"登录","没有此用户",NULL,true);
        lv_obj_align(mbox,LV_ALIGN_TOP_MID,0,100);
        lv_obj_set_style_text_font(mbox, chinese_font, 0);
        lv_obj_center(mbox);

        printf("用户不存在\n");
    }
    flag=false;
}
static void mbox_ok_event_cb(lv_event_t * e) {
    lv_obj_t * btnmatrix = lv_event_get_target(e);       // 事件触发者是按钮矩阵
    lv_obj_t * mbox = lv_obj_get_parent(btnmatrix);      // 获取消息框对象
    const char * txt = lv_msgbox_get_active_btn_text(mbox);

    if(txt != NULL && strcmp(txt, "ok") == 0) {
        lv_obj_del(mbox);   // 删除
        printf("ok pressed, msgbox closed\n");
    }
}




//注册
void btn1_event_cb(lv_event_t *e) {
     login_input_t *inputs=lv_event_get_user_data(e);
     const char *username= lv_textarea_get_text(inputs->ta_username);
     const char *password= lv_textarea_get_text(inputs->ta_password);
     for(int i=0;i<10;i++){
         if(strcmp(username,users[i].name)==0){
          lv_obj_t *mbox=lv_msgbox_create(lv_scr_act(),"创建用户","用户已存在",NULL,true);
          lv_obj_align(mbox,LV_ALIGN_TOP_MID,0,100);
          lv_obj_set_style_text_font(mbox, chinese_font, 0);
          lv_obj_center(mbox);
             printf("用户已存在\n");
             return ;
         }
     }
     
     if(save<MAX_USER_NUM){
        user u;
        strcpy(u.name,username);
        strcpy(u.password,password);
        push_user_file(u);
         strcpy(users[count].name,username);
         strcpy(users[count].password,password);
         users[count].flag=0;
         count++;
         lv_obj_t *mbox=lv_msgbox_create(lv_scr_act(),"创建用户","存储成功",NULL,true);
         lv_obj_align(mbox,LV_ALIGN_TOP_MID,0,100);
         lv_obj_set_style_text_font(mbox, chinese_font, 0);
         lv_obj_center(mbox);
         printf("存储成功\n");
     }else{
         lv_obj_t *mbox=lv_msgbox_create(lv_scr_act(),"创建用户","用户满！",NULL,true);
         lv_obj_align(mbox,LV_ALIGN_TOP_MID,0,100);
         lv_obj_set_style_text_font(mbox, chinese_font, 0);
         lv_obj_center(mbox);
         printf("用户已满\n");
     }

}


//点击非键盘区域隐藏
 void hide_kb_on_click(lv_event_t * e) {
    lv_obj_t * kb = lv_event_get_user_data(e);  // kb 对象
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);    // 隐藏键盘
}

void setup_hide_kb(lv_obj_t * kb) {
    lv_obj_add_event_cb(lv_scr_act(), hide_kb_on_click, LV_EVENT_CLICKED, kb);
}


void ta_event_cb(lv_event_t *e){
  lv_obj_t *ta = lv_event_get_target(e);
  lv_keyboard_set_textarea(kb, ta);
  lv_obj_clear_flag(kb,LV_OBJ_FLAG_HIDDEN);
}

void ta_event_hide_cb(lv_event_t *e){
  lv_event_code_t code = lv_event_get_code(e);
  if(code ==LV_EVENT_READY||code == LV_EVENT_CANCEL){
    lv_obj_add_flag(kb,LV_OBJ_FLAG_HIDDEN);
  } 
}

void ta_event_hide_cb_admin(lv_event_t *e){
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_READY || code == LV_EVENT_CANCEL || code == LV_EVENT_DEFOCUSED) {
        if(kb3) lv_obj_add_flag(kb3, LV_OBJ_FLAG_HIDDEN);
    }
}


void ta_event_cb3(lv_event_t *e){
  lv_obj_t *ta = lv_event_get_target(e);
  lv_keyboard_set_textarea(kb3, ta);
  lv_obj_clear_flag(kb3,LV_OBJ_FLAG_HIDDEN);
}

void ta_event_hide_cb1(lv_event_t *e){
  lv_event_code_t code = lv_event_get_code(e);
  if(code ==LV_EVENT_READY||code == LV_EVENT_CANCEL){
    lv_obj_add_flag(kb3,LV_OBJ_FLAG_HIDDEN);
  } 
}


// 点击小眼睛的回调函数
static void pwd_eye_event_cb(lv_event_t * e)
{
    lv_obj_t *btn = lv_event_get_target(e);
    if(pwd_hidden) {
        // 显示明文
        lv_textarea_set_password_mode(ta2, false);
        lv_obj_set_style_bg_color(btn, lv_palette_main(LV_PALETTE_GREEN), 0); // 按钮变绿提示
    } else {
        // 隐藏（用 * 替代）
        lv_textarea_set_password_mode(ta2, true);
        lv_textarea_set_password_bullet(ta2, "*");
        lv_obj_set_style_bg_color(btn, lv_palette_main(LV_PALETTE_GREY), 0); // 按钮恢复灰色
    }
    pwd_hidden = !pwd_hidden; // 状态翻转
}

void create_ui(void) {
    lv_obj_t *scr_t = lv_scr_act();
    lv_obj_t *label_t =lv_label_create(scr_t);
    lv_label_set_text(label_t,"欢迎登录地铁购票系统");
    lv_obj_align(label_t,LV_ALIGN_TOP_MID,0,50);
    lv_obj_set_style_text_font(label_t,chinese_font,0);
    
    //lv_obj_set_style_text_font(label_t,LV_FONT_MONTSERRAT_22,LV_PART_MAIN);
    set_time();
    // 输入框一
    orig_scr = lv_scr_act();
    login_input_t *login_input=malloc(sizeof(login_input_t));
    lv_obj_t *ta = lv_textarea_create(lv_scr_act());
    lv_obj_set_size(ta, 400, 50);
    lv_obj_align(ta, LV_ALIGN_TOP_MID, 0, 140);
    lv_textarea_set_placeholder_text(ta, "用户名");
    lv_obj_set_style_text_font(ta, chinese_font, 0);
    //输入框二
    ta2= lv_textarea_create(lv_scr_act());
    lv_obj_set_size(ta2,400,50);
    lv_obj_align(ta2,LV_ALIGN_TOP_MID,0,200);
    lv_textarea_set_placeholder_text(ta2,"密码");
    lv_obj_set_style_text_font(ta2,chinese_font,0);
    lv_textarea_set_password_mode(ta2, true);
    lv_textarea_set_password_bullet(ta2, "*");

     // 创建小眼睛按钮
    lv_obj_t *btn_eye = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn_eye, 60, 50);
    lv_obj_align_to(btn_eye, ta2, LV_ALIGN_OUT_RIGHT_MID, 10, 0); // 放在输入框右边
    lv_obj_set_style_bg_color(btn_eye, lv_palette_main(LV_PALETTE_GREY), 0);
    lv_obj_t *label_eye = lv_label_create(btn_eye);
    lv_label_set_text(label_eye, "#");
    lv_obj_center(label_eye);
    lv_obj_add_event_cb(btn_eye, pwd_eye_event_cb, LV_EVENT_CLICKED, NULL);

    login_input->ta_username=ta;
    login_input->ta_password=ta2;
        // 按钮1
    lv_obj_t *btn = lv_btn_create(lv_scr_act());
    lv_obj_align(btn, LV_ALIGN_TOP_MID, -150, 300);
    lv_obj_t *btn_label = lv_label_create(btn);
    lv_label_set_text(btn_label, "登录");
    lv_obj_set_style_text_font(btn_label, chinese_font, 0);
    lv_obj_center(btn_label);//字体的居中

    // 按钮2
    lv_obj_t *btn2 = lv_btn_create(lv_scr_act());
    lv_obj_align(btn2, LV_ALIGN_TOP_MID, 130, 300);
    lv_obj_t *btn_label1 = lv_label_create(btn2);
    lv_label_set_text(btn_label1, "创建用户");
    lv_obj_set_style_text_font(btn_label1, chinese_font, 0);
    lv_obj_center(btn_label1);


    // 绑定事件，把 textarea 传给回调
    lv_obj_add_event_cb(btn, btn_event_cb, LV_EVENT_CLICKED, login_input);
    lv_obj_add_event_cb(btn2,btn1_event_cb,LV_EVENT_CLICKED,login_input);


    kb = lv_keyboard_create(lv_scr_act());
    lv_obj_align(kb,LV_ALIGN_BOTTOM_MID,0,0);
    lv_keyboard_set_textarea(kb,ta);
    setup_hide_kb(kb);

    lv_obj_add_flag(kb,LV_OBJ_FLAG_HIDDEN);

lv_obj_add_event_cb(ta, ta_event_cb, LV_EVENT_FOCUSED, NULL);
lv_obj_add_event_cb(ta2, ta_event_cb, LV_EVENT_FOCUSED, NULL);

// 键盘输入完成/取消时隐藏
lv_obj_add_event_cb(kb, ta_event_hide_cb, LV_EVENT_READY, NULL);
lv_obj_add_event_cb(kb, ta_event_hide_cb, LV_EVENT_CANCEL, NULL);
lv_obj_add_event_cb(kb,ta_event_hide_cb,LV_EVENT_DEFOCUSED,NULL);




printf("创建界面没问题");
fflush(stdout);

}

void timer_cb(lv_timer_t *timer){
  lv_obj_t *label2=timer->user_data;
  LV_UNUSED(timer);
    time_t now=time(NULL);
    struct tm *t=localtime(&now);
    char buf[20];
    sprintf(buf,"%04d-%02d-%02d %02d:%02d:%02d",
      t->tm_year+1900,
      t->tm_mon+1,
      t->tm_mday,
      t->tm_hour,
      t->tm_min,
      t->tm_sec
    );
    lv_label_set_text(label2,buf);
    
}


void show_image_auto(const char *path)
{
    lv_obj_t *img = lv_img_create(lv_scr_act());
    lv_img_set_src(img, path);
    if(lv_img_get_src(img) == NULL) {
        printf("图片加载失败: %s\n", path);
        lv_obj_del(img);
        return;
    }
    printf("图片加载成功: %s\n", path);

    lv_coord_t scr_w = lv_disp_get_hor_res(NULL);
    lv_coord_t scr_h = lv_disp_get_ver_res(NULL);

    // 设置图片大小为屏幕尺寸 
    lv_obj_set_size(img, scr_w, scr_h);

    lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
}


void set_time(){
    lv_obj_t *label=lv_label_create(lv_scr_act());
  lv_obj_align(label,LV_ALIGN_CENTER,0,0);
  lv_obj_set_style_text_font(label, &lv_font_montserrat_28, 0);
  lv_timer_create(timer_cb,1000,label);
  lv_obj_align(label, LV_ALIGN_TOP_RIGHT,0,0);
}

void return_add_picture_page(lv_event_t *e){
  lv_scr_load(add_picture_page);
}



bool file_exists_in_dir(const char *dir_path,const char* filename){
  DIR *dir = opendir(dir_path);
  if(!dir){return false;}
  struct dirent *entry;
  while((entry=readdir(dir))!=NULL){
    if(strcmp(entry->d_name,filename)==0){
      closedir(dir);
      return true;
    }
  }
  closedir(dir);
  return false;
}
//---------------------------
void return_add_picture_page1(lv_event_t *e){
  lv_scr_load(add_picture_file_path_use);
}

void add_picture_file_path(lv_event_t *e) {
    add_picture_data_t *data = (add_picture_data_t *)lv_event_get_user_data(e);
    if(data == NULL) return;

    // 获取输入框内容
    const char *input_txt = lv_textarea_get_text(data->ta);
    printf("原始txt: %s\n", data->txt);
    printf("输入框txt: %s\n", input_txt);

    // 写入文件
    FILE *fp = fopen("/picture/picture.txt", "a+");
    if(fp){
      if(file_exists_in_dir("/picture",input_txt)){
        fprintf(fp, "%s %s %d\n", input_txt, data->txt, 1);

            lv_obj_t *msgbox2 = lv_msgbox_create(lv_scr_act()," ","图片添加成功",NULL,true);
            lv_obj_set_style_text_font(msgbox2,chinese_font,0);
            lv_obj_align(msgbox2,LV_ALIGN_CENTER,0,0);

              lv_obj_t *btn=lv_btn_create(msgbox2);
              lv_obj_set_size(btn,100,50);
              lv_obj_t *label3=lv_label_create(btn);
              lv_label_set_text(label3,"返回");
              lv_obj_set_style_text_font(label3,chinese_font,0);
              lv_obj_align(btn,LV_ALIGN_TOP_RIGHT,0,0);
              lv_obj_add_event_cb(btn, return_add_picture_page1, LV_EVENT_CLICKED, NULL);
      }else{
            lv_obj_t *msgbox1 = lv_msgbox_create(lv_scr_act()," ","图片不存在,添加失败",NULL,true);
            lv_obj_set_style_text_font(msgbox1,chinese_font,0);
            lv_obj_align(msgbox1,LV_ALIGN_CENTER,0,0);


            lv_obj_t *btn1=lv_btn_create(msgbox1);
              lv_obj_set_size(btn1,100,50);
              lv_obj_t *label4=lv_label_create(btn1);
              lv_label_set_text(label4,"返回");
              lv_obj_set_style_text_font(label4,chinese_font,0);
              lv_obj_align(btn1,LV_ALIGN_TOP_RIGHT,0,0);
              lv_obj_add_event_cb(btn1, return_add_picture_page1, LV_EVENT_CLICKED, NULL);
      }


        
        fclose(fp);
        
    }

    // 释放
    free(data);
}

//-------------------------------------------
void add_new_picture_page(lv_event_t *e){
  char *txt=(char *)lv_event_get_user_data(e);

//create
  lv_obj_t *page_new=lv_obj_create(NULL);
  lv_obj_set_size(page_new,LV_HOR_RES,LV_VER_RES);
  lv_obj_t *label = lv_label_create(page_new);
  lv_scr_load(page_new);  
  show_image_auto("/picture/admin.bmp");
  lv_label_set_text(label,"选择图片界面");
  lv_obj_align(label,LV_ALIGN_TOP_MID,0,50);
  lv_obj_set_style_text_font(label,chinese_font,0);


//----------------------------------------------

// 输入框
    lv_obj_t *ta = lv_textarea_create(lv_scr_act());
    lv_obj_set_size(ta, 400, 50);
    lv_obj_align(ta, LV_ALIGN_TOP_MID, 0, 140);
    lv_textarea_set_placeholder_text(ta, "图片路径");
    lv_obj_set_style_text_font(ta, chinese_font, 0);

// 路径提交按钮
lv_obj_t *btn3 = lv_btn_create(lv_scr_act());
lv_obj_set_size(btn3, 150, 50);
lv_obj_align(btn3, LV_ALIGN_TOP_MID, 0, 200); 

// 按钮文字
lv_obj_t *label3 = lv_label_create(btn3);
lv_label_set_text(label3, "添加");
lv_obj_set_style_text_font(label3, chinese_font, 0);
lv_obj_center(label3);  // 文字居中


// 创建事件数据
add_picture_data_t *data = malloc(sizeof(add_picture_data_t));
data->ta = ta;   // 当前输入框
printf("txt: %s\n", txt);
data->txt = txt; // 传入的 txt

// 绑定按钮事件
lv_obj_add_event_cb(btn3, add_picture_file_path, LV_EVENT_CLICKED, data);


 
    //键盘
    kb3 = lv_keyboard_create(lv_scr_act());
    lv_obj_align(kb3,LV_ALIGN_BOTTOM_MID,0,0);
    lv_keyboard_set_textarea(kb3,ta);
    

    lv_obj_add_flag(kb3,LV_OBJ_FLAG_HIDDEN);

lv_obj_add_event_cb(ta, ta_event_cb3, LV_EVENT_FOCUSED, NULL);

// 键盘输入完成/取消时隐藏
lv_obj_add_event_cb(kb3, ta_event_hide_cb_admin, LV_EVENT_READY, NULL);
lv_obj_add_event_cb(kb3, ta_event_hide_cb_admin, LV_EVENT_CANCEL, NULL);
lv_obj_add_event_cb(kb3,ta_event_hide_cb_admin,LV_EVENT_DEFOCUSED,NULL);
setup_hide_kb(kb3);
//--------------------------------------------



  lv_obj_t *btn2=lv_btn_create(lv_scr_act());
  lv_obj_set_size(btn2,100,50);
  lv_obj_t *label2=lv_label_create(btn2);
  lv_label_set_text(label2,"返回");
  lv_obj_set_style_text_font(label2,chinese_font,0);
  lv_obj_align(btn2,LV_ALIGN_TOP_RIGHT,0,0);
  lv_obj_add_event_cb(btn2, return_add_picture_page, LV_EVENT_CLICKED, NULL);
}

// 消息盒子响应
void msgbox_btn_event_cb(lv_event_t *e) {
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_VALUE_CHANGED) {
        lv_obj_t *msgbox = lv_event_get_target(e); // 获取触发事件的消息盒子
        const char *btn_txt = lv_msgbox_get_active_btn_text(msgbox);
        if(strcmp(btn_txt, "确定") == 0) {
            lv_obj_del(msgbox); // 点击确定删除消息盒子
        }
    }
}


//为不存在图片的站点添加图片
void add_picture_ways(char *txt){
    add_picture_file_path_use=lv_scr_act();
    char *txt_copy = malloc(strlen(txt) + 1); // +1 放 '\0'
    strcpy(txt_copy, txt);

    char txt_use[64];
    sprintf(txt_use,"此站点%s没有图片,是否添加?",txt);

    lv_obj_t *msgbox = lv_msgbox_create(lv_scr_act()," ",txt_use,NULL,true);
    lv_obj_set_style_text_font(msgbox,chinese_font,0);
    lv_obj_align(msgbox,LV_ALIGN_CENTER,0,0);

    lv_obj_t *btn2=lv_btn_create(msgbox);
    lv_obj_set_size(btn2,100,50);
    lv_obj_t *label2=lv_label_create(btn2);
    lv_label_set_text(label2,"是");
    lv_obj_set_style_text_font(label2,chinese_font,0);
    lv_obj_align(btn2,LV_ALIGN_TOP_MID,0,150);

    lv_obj_add_event_cb(btn2, add_new_picture_page, LV_EVENT_CLICKED, txt_copy);
}



// 定义全局或局部变量
lv_obj_t *dropdown_place;

void show_admin_picture(lv_event_t *e){
    char txt[64];
    uint16_t sel = lv_dropdown_get_selected(dropdown_place);
    lv_dropdown_get_selected_str(dropdown_place, txt, sizeof(txt));

    int found = set_file_flag(txt, 1);
    if(found == 1){
        add_picture_ways(txt);
    }
}


void unshow_admin_picture(lv_event_t *e){
  char txt[64]; // buffer，长度根据你选项最大长度设置
uint16_t sel = lv_dropdown_get_selected(dropdown_place); // 获取索引
lv_dropdown_get_selected_str(dropdown_place, txt, sizeof(txt)); // 写入 txt
  int found=set_file_flag(txt,0);
  if(found==1){
      add_picture_ways(txt);
  }
}


static void create_admin_recommend_ui() {
    lv_obj_t *page_admin=lv_obj_create(NULL);
  lv_obj_set_size(page_admin,LV_HOR_RES,LV_VER_RES);
  lv_obj_t *label = lv_label_create(page_admin);
  lv_scr_load(page_admin);  
  show_image_auto("/picture/admin.bmp");
  lv_label_set_text(label,"选择展示图片界面");
  lv_obj_align(label,LV_ALIGN_TOP_MID,0,50);
  lv_obj_set_style_text_font(label,chinese_font,0);

  lv_obj_t *btn2=lv_btn_create(lv_scr_act());
  lv_obj_set_size(btn2,100,50);
  lv_obj_t *label2=lv_label_create(btn2);
  lv_label_set_text(label2,"返回");
  lv_obj_set_style_text_font(label2,chinese_font,0);
  lv_obj_align(btn2,LV_ALIGN_TOP_RIGHT,0,0);
  lv_obj_add_event_cb(btn2, return_admin_page2, LV_EVENT_CLICKED, NULL);





    // 创建下拉列表
    dropdown_place = lv_dropdown_create(page_admin);
    
    // 构建地点字符串
    char places_str[512] = {0};
    int offset = 0;

    for (int i = 0; i < place_count; i++) {
        offset += snprintf(places_str + offset,
                       sizeof(places_str) - offset,
                       "%s%s",
                       place_map[i].name,
                       (i != place_count - 1) ? "\n" : "");
        }


    lv_dropdown_set_options(dropdown_place, places_str);
    lv_obj_align(dropdown_place, LV_ALIGN_TOP_MID, 0, 150);
    lv_obj_set_width(dropdown_place, 200);

    // 设置主体部分字体
    lv_obj_set_style_text_font(dropdown_place, chinese_font, LV_PART_MAIN);

    // 设置选项列表部分字体
    lv_obj_t *list = lv_dropdown_get_list(dropdown_place);
    lv_obj_set_style_text_font(list, chinese_font, LV_PART_MAIN);

    
    lv_dropdown_set_selected(dropdown_place, 0);
    lv_obj_add_event_cb(dropdown_place, admin_dropdown_event_cb, LV_EVENT_VALUE_CHANGED, NULL);

    //按钮一:展示图片


    add_picture_page=lv_scr_act();
      lv_obj_t *btn3=lv_btn_create(lv_scr_act());
      lv_obj_set_size(btn3,100,50);
      lv_obj_t *label3=lv_label_create(btn3);
      lv_label_set_text(label3,"展示");
      lv_obj_set_style_text_font(label3,chinese_font,0);
      lv_obj_align(btn3,LV_ALIGN_TOP_MID,200,250);
      lv_obj_add_event_cb(btn3, show_admin_picture, LV_EVENT_CLICKED, NULL);
      //  set_file_flag(const char *filename,int newflag);

    //按钮二: 取消展示
      lv_obj_t *btn4=lv_btn_create(lv_scr_act());
      lv_obj_set_size(btn4,100,50);
      lv_obj_t *label4=lv_label_create(btn4);
      lv_label_set_text(label4,"不展示");
      lv_obj_set_style_text_font(label4,chinese_font,0);
      lv_obj_align(btn4,LV_ALIGN_TOP_MID,-200,250);
      lv_obj_add_event_cb(btn4, unshow_admin_picture, LV_EVENT_CLICKED, NULL);

}


// 下拉列表事件回调
static void admin_dropdown_event_cb(lv_event_t *e) {
    lv_obj_t *dd = lv_event_get_target(e);
    uint16_t sel = lv_dropdown_get_selected(dd); // 获取选择索引
    printf("管理员选择推荐地点：%s\n", place_map[sel].name);
    

}

void return_admin_page2(lv_event_t *e){
  lv_scr_load(page_admin2);
}

void go_to_del_user_page(lv_event_t *e){
    create_page_admin();
}

void go_to_change_picture_page(lv_event_t *e){
  create_admin_recommend_ui();
}

void create_page_admin_2(){
  lv_obj_t *page_admin=lv_obj_create(NULL);
  lv_obj_set_size(page_admin,LV_HOR_RES,LV_VER_RES);
  lv_obj_t *label = lv_label_create(page_admin);
  lv_scr_load(page_admin);  
  lv_label_set_text(label,"欢迎管理员登录地铁购票系统");
  lv_obj_align(label,LV_ALIGN_TOP_MID,0,50);
  lv_obj_set_style_text_font(label,chinese_font,0);

  page_admin2=lv_scr_act();
  lv_obj_t *btn2=lv_btn_create(lv_scr_act());
  lv_obj_set_size(btn2,100,50);
  lv_obj_t *label2=lv_label_create(btn2);
  lv_label_set_text(label2,"返回");
  lv_obj_set_style_text_font(label2,chinese_font,0);
  lv_obj_align(btn2,LV_ALIGN_TOP_RIGHT,0,0);
  lv_obj_add_event_cb(btn2, return_main_page, LV_EVENT_CLICKED, NULL);

  
  lv_obj_t *btn3=lv_btn_create(lv_scr_act());
  lv_obj_set_size(btn3,200,60);
  lv_obj_t *label3=lv_label_create(btn3);
  lv_label_set_text(label3,"删除用户界面");
  lv_obj_set_style_text_font(label3,chinese_font,0);
  lv_obj_align(btn3,LV_ALIGN_TOP_RIGHT,-100,200);
  lv_obj_add_event_cb(btn3, go_to_del_user_page, LV_EVENT_CLICKED, NULL);


  lv_obj_t *btn4=lv_btn_create(lv_scr_act());
  lv_obj_set_size(btn4,200,60);
  lv_obj_t *label4=lv_label_create(btn4);
  lv_label_set_text(label4,"下拉列表界面");
  lv_obj_set_style_text_font(label4,chinese_font,0);
  lv_obj_align(btn4,LV_ALIGN_TOP_LEFT,100,200);
  lv_obj_add_event_cb(btn4, go_to_change_picture_page, LV_EVENT_CLICKED, NULL);

}









//删除用户
void create_page_admin(){
  lv_obj_t *page_admin=lv_obj_create(NULL);
  lv_obj_set_size(page_admin,LV_HOR_RES,LV_VER_RES);
  lv_obj_t *label = lv_label_create(page_admin);
  lv_scr_load(page_admin);  
  show_image_auto("/picture/admin.bmp");
  lv_label_set_text(label,"删除用户界面");
  lv_obj_align(label,LV_ALIGN_TOP_MID,0,50);
  lv_obj_set_style_text_font(label,chinese_font,0);

  lv_obj_t *btn2=lv_btn_create(lv_scr_act());
  lv_obj_set_size(btn2,100,50);
  lv_obj_t *label2=lv_label_create(btn2);
  lv_label_set_text(label2,"返回");
  lv_obj_set_style_text_font(label2,chinese_font,0);
  lv_obj_align(btn2,LV_ALIGN_TOP_RIGHT,0,0);
  lv_obj_add_event_cb(btn2, return_admin_page2, LV_EVENT_CLICKED, NULL);


 // 输入框
    lv_obj_t *ta = lv_textarea_create(lv_scr_act());
    lv_obj_set_size(ta, 400, 50);
    lv_obj_align(ta, LV_ALIGN_TOP_MID, -75, 90);
    lv_textarea_set_placeholder_text(ta, "用户名");
    lv_obj_set_style_text_font(ta, chinese_font, 0);
    

// 删除按钮
lv_obj_t *btn3 = lv_btn_create(lv_scr_act());
lv_obj_set_size(btn3, 150, 50);
lv_obj_align(btn3, LV_ALIGN_TOP_MID, 200, 90); // 偏移量调整到可视范围

// 按钮文字
lv_obj_t *label3 = lv_label_create(btn3);
lv_label_set_text(label3, "删除用户");
lv_obj_set_style_text_font(label3, chinese_font, 0);
lv_obj_center(label3);  // 文字居中

// 绑定事件，并把输入框 ta 传入
lv_obj_add_event_cb(btn3, delete_user_event_cb, LV_EVENT_CLICKED, ta);
 
    //键盘
    kb3_1 = lv_keyboard_create(lv_scr_act());
    lv_obj_align(kb3_1,LV_ALIGN_BOTTOM_MID,0,0);
    lv_keyboard_set_textarea(kb3_1,ta);
    setup_hide_kb(kb3_1);

    lv_obj_add_flag(kb3_1,LV_OBJ_FLAG_HIDDEN);

lv_obj_add_event_cb(ta, ta_event_cb3_1, LV_EVENT_FOCUSED, NULL);

// 键盘输入完成/取消时隐藏
lv_obj_add_event_cb(kb3_1, ta_event_hide_cb_admin_1, LV_EVENT_READY, NULL);
lv_obj_add_event_cb(kb3_1, ta_event_hide_cb_admin_1, LV_EVENT_CANCEL, NULL);
lv_obj_add_event_cb(kb3_1,ta_event_hide_cb_admin_1,LV_EVENT_DEFOCUSED,NULL);

//更改用户输入框--------------------------------------------------------


//更改密码
lv_obj_t *ta_new = lv_textarea_create(lv_scr_act());
    lv_obj_set_size(ta_new, 400, 50);
    lv_obj_align(ta_new, LV_ALIGN_TOP_MID, -75, 170);
    lv_textarea_set_placeholder_text(ta_new, "新密码");
    lv_obj_set_style_text_font(ta_new, chinese_font, 0);


login_input_t *ldata = malloc(sizeof(login_input_t));
ldata->ta_username = ta;
ldata->ta_password = ta_new;
    // 更改按钮
lv_obj_t *btn4 = lv_btn_create(lv_scr_act());
lv_obj_set_size(btn4, 150, 50);
lv_obj_align(btn4, LV_ALIGN_TOP_MID, 200, 170);

lv_obj_t *label4 = lv_label_create(btn4);
lv_label_set_text(label4, "修改密码");
lv_obj_set_style_text_font(label4, chinese_font, 0);
lv_obj_center(label4);

// 绑定事件，传 ldata
lv_obj_add_event_cb(btn4, change_user_event_cb, LV_EVENT_CLICKED, ldata);
 
    //键盘
    lv_keyboard_set_textarea(kb3_1,ta);
    setup_hide_kb(kb3_1);

    lv_obj_add_flag(kb3_1,LV_OBJ_FLAG_HIDDEN);

lv_obj_add_event_cb(ta_new, ta_event_cb3_1, LV_EVENT_FOCUSED, NULL);


}


void change_user_event_cb(lv_event_t *e) {
    login_input_t *ldata = lv_event_get_user_data(e);
    const char *username = lv_textarea_get_text(ldata->ta_username);
    const char *newpwd   = lv_textarea_get_text(ldata->ta_password);

    if(strlen(username) == 0 || strlen(newpwd) == 0) {
        lv_obj_t *mbox = lv_msgbox_create(lv_scr_act(), "修改密码", "用户名或新密码不能为空", NULL, true);
        lv_obj_set_style_text_font(mbox, chinese_font, 0);
        lv_obj_center(mbox);
        return;
    }

    int found = 0;
    for(int i = 0; i < count; i++) {
        if(strcmp(users[i].name, username) == 0) {
            strcpy(users[i].password, newpwd);
            found = 1;
            break;
        }
    }

    if(found) {
        // 重写 user.txt
        FILE *fp = fopen("/picture/user.txt", "w");
        if(fp) {
            for(int i = 0; i < count; i++) {
                fprintf(fp, "%s %s\n", users[i].name, users[i].password);
            }
            fclose(fp);
        }

        lv_obj_t *mbox = lv_msgbox_create(lv_scr_act(), "修改密码", "密码修改成功", NULL, true);
        lv_obj_set_style_text_font(mbox, chinese_font, 0);
        lv_obj_center(mbox);
        printf("用户 %s 的密码已修改为 %s\n", username, newpwd);
    } else {
        lv_obj_t *mbox = lv_msgbox_create(lv_scr_act(), "修改密码", "未找到该用户", NULL, true);
        lv_obj_set_style_text_font(mbox, chinese_font, 0);
        lv_obj_center(mbox);
        printf("未找到用户 %s\n", username);
    }
}





void ta_event_hide_cb_admin_1(lv_event_t *e){
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_READY || code == LV_EVENT_CANCEL || code == LV_EVENT_DEFOCUSED) {
        if(kb3_1) lv_obj_add_flag(kb3_1, LV_OBJ_FLAG_HIDDEN);
    }
}


void ta_event_cb3_1(lv_event_t *e){
  lv_obj_t *ta = lv_event_get_target(e);
  lv_keyboard_set_textarea(kb3_1, ta);
  lv_obj_clear_flag(kb3_1,LV_OBJ_FLAG_HIDDEN);
}


void delete_user_event_cb(lv_event_t *e){
  lv_obj_t *ta = (lv_obj_t*)lv_event_get_user_data(e);
  const char *username = lv_textarea_get_text(ta);

  if(strlen(username)>0){
    delete_user(username);
  }else{
    printf("请输入用户名");
    fflush(stdout);
  }
}
//没有真正删掉---------------------------------------------------------------
void delete_user(const char *username){
    printf("用户%s\n", username);
    fflush(stdout);

    FILE *fp = fopen("/picture/user.txt", "r+");
    if (!fp) {
        perror("open user.txt error");
        fflush(stdout);
        return;
    }

    user users_temp[MAX_USER_NUM];
    int count_temp = 0;

    while (fscanf(fp, "%s %s", users_temp[count_temp].name, users_temp[count_temp].password) == 2) {
        count_temp++;
    }
    fclose(fp);

    fp = fopen("/picture/user.txt", "w");
    if (!fp) {
        perror("open user.txt error");
        fflush(stdout);
        return;
    }

    int found = 0;
    if (strcmp(username, "admin") == 0) {
        printf("不能删除 admin 用户\n");
        fflush(stdout);
        fclose(fp);
        return;
    }

    for (int i = 0; i < count_temp; i++) {
        if (strcmp(users_temp[i].name, username) == 0) {
            found = 1;
            continue;  // 跳过这个用户，相当于删除
        }
        fprintf(fp, "%s %s\n", users_temp[i].name, users_temp[i].password);
    }
    fclose(fp);

    if (found) {
        // 同步删除全局 users[] 数组中的该用户
        for (int i = 0; i < count; i++) {
            if (strcmp(users[i].name, username) == 0) {
                // 从 i 位置往前挪动
                for (int j = i; j < count - 1; j++) {
                    users[j] = users[j + 1];
                }
                count--;  // 用户总数减 1
                break;
            }
        }
        printf("用户 %s 删除成功\n", username);
    } else {
        printf("未找到用户 %s\n", username);
    }
}
